<?php

setlocale(LC_ALL, "ru_RU.UTF-8");
mb_internal_encoding("UTF-8");

$curRealDir = mb_substr($_SERVER["SCRIPT_FILENAME"], 0, mb_strrpos($_SERVER["SCRIPT_FILENAME"], "/"));
$curRealDir = mb_substr($curRealDir, 0, mb_strrpos($curRealDir, "/"));

require_once ("./libs/utils.inc.php");
require_once ("./config/setup.php");

foreach ($_FILES as $filePostName => $fileProps) {
    if ($fileProps['error'] == 0) {
        if (substr($fileProps['type'], 0, 5) == "image") {
            $imagePath = getNextImagePath($curRealDir, $fileProps['name']);
            if (move_uploaded_file($fileProps['tmp_name'], $curRealDir . $imagePath)) {
                echo $imagePath;
            } else {
                echo "can not copy file. hosting problem.";
            }
        } else {
            echo "extension not supported.";
        }
    } else {
        echo "error uploading the file.";
    }
}
?>
